package com.jiazhong.mingxing.boot.boot12.controller;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RequestMapping("/provider")
@RestController
@Slf4j
public class ProviderController {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @SneakyThrows
    // 发送同步消息1
    @RequestMapping("/send_sync_1")
    public String sendSync1(@RequestParam("body") String body) {
        // 产生一个消息
        Message message = new Message("TopicA", "sync1", body.getBytes(StandardCharsets.UTF_8));
        // 发送消息
        SendResult sendResult = rocketMQTemplate.getProducer().send(message);
        log.info("sendResult={}", sendResult);
        return sendResult.getSendStatus().toString();
    }

    // 发送同步消息2
    @RequestMapping("/send_sync_2")
    public String sendSync2(@RequestParam("body") String body) {
        SendResult sendResult = rocketMQTemplate.syncSend("TopicA:sync2", body);
        log.info("sendResult={}", sendResult);
        return sendResult.getSendStatus().toString();
    }

    @SneakyThrows
    @RequestMapping("/send_async_1")
    public String sendAsync1(@RequestParam("body") String body) {
        Message message = new Message("TopicA", "async", body.getBytes(StandardCharsets.UTF_8));
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        final String[] result = {""};
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                result[0] = sendResult.getSendStatus().toString();
            }

            @Override
            public void onException(Throwable throwable) {
                result[0] = throwable.getMessage();
            }
        });
        Thread.sleep(3000);
        return result[0];
    }

    @SneakyThrows
    @RequestMapping("/send_async_2")
    public String sendAsync2(@RequestParam("body") String body) {
        final String[] result = {""};
        rocketMQTemplate.asyncSend("TopicA:async", body, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                result[0] = sendResult.getSendStatus().toString();
            }

            @Override
            public void onException(Throwable throwable) {
                result[0] = throwable.getMessage();
            }
        });
        Thread.sleep(3000);
        return result[0];
    }

    @SneakyThrows
    @RequestMapping("/send_oneway_1")
    public String sendOneway1(@RequestParam("body") String body) {
        Message message = new Message("TopicA", "oneway", body.getBytes(StandardCharsets.UTF_8));
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.sendOneway(message);
        return "success";
    }

    @SneakyThrows
    @RequestMapping("/send_oneway_2")
    public String sendOneway2(@RequestParam("body") String body) {
        rocketMQTemplate.sendOneWay("TopicA:oneway", body);
        return "success";
    }

}
